package sk.dvsk.maven.doxia.macro;

import org.apache.maven.doxia.macro.AbstractMacro;
import org.apache.maven.doxia.macro.MacroExecutionException;
import org.apache.maven.doxia.macro.MacroRequest;
import org.apache.maven.doxia.sink.Sink;

/**
 * @plexus.component role="org.apache.maven.doxia.macro.Macro"
 *                   role-hint="enable-highlighter"
 */
public class EnableHighlighterMacro extends AbstractMacro {

	public static final String DEFAULT_URL = "syntaxhighlighter/";
	private static final String [] modules = "shCore,shBrushCss,shBrushJScript,shBrushJava,shBrushSql,shBrushXml".split(",");
	
	public void execute(Sink sink, MacroRequest request) throws MacroExecutionException {
		
		String url = (String) request.getParameter("url");
		
		if(url == null) url = DEFAULT_URL;
		else if(!url.endsWith("/")) url += "/";
		
		sink.rawText("<!-- syntax highlighter -->\n");
		sink.rawText("<link type=\"text/css\" rel=\"stylesheet\" href=\"" + url + "SyntaxHighlighter.css\"></link>\n");
		for(int i = 0; i < modules.length; i++)
			sink.rawText("<script language=\"javascript\" src=\"" + url + modules[i] + ".js\"></script>\n");
		sink.rawText("<script language=\"javascript\">\n");
		sink.rawText("\tdp.SyntaxHighlighter.HighlightAll('code');\n");
		sink.rawText("</script>\n");
		sink.rawText("\n");
	}

}
